<?php 
	namespace Admin\Model;
	use Think\Model;
	class GoodsModel extends Model{
		//造一个受保护属性
			protected $_validate=array(
				array('name','require','商品名不能为空'),
				array('price','require','价格不能为空'),
				array('stock','require','库存不能为空')
				);
			protected $_auto=array(
				//1是新增数据的时候处理
				array('addtime','time',1,'function')
				);
		public function getAll($name,$limit){
			$list=$this->field('
				g.id 		gid,
				g.name      gname,
				g.is_new    gnew,
				g.is_hot    ghot,
				g.is_bast   gb,
				g.price     gp,
				g.stock     gs,
				g.description gd,
				g.addtime   gtime,
				gi.name     giname,
				c.name      cname')
			->table('goods 		g,
					goods_img   gi,
					category 	c')
			->where("g.id=gi.goods_id
					and
					c.id=g.cate_id	
					and
					gi.is_cover=1 and g.name like'%$name%'")
			->limit($limit)
			->select();
			return $list;

		}

		public function setAdd($data,$filename){
			$this->startTrans();
			$id=$this->add($data);
			
			$model_img=M('goodsImg');
			$data['goods_id']=$id;
			$data['is_cover']=1;
			$data['name']=$filename;

			$res=$model_img->data($data)->add();
			if ($res) {
				$this->commit();
				return true;
			}else{
				$this->rollback();
				return false;
			}
		}

		public function getDetail($id)
		{
			$list=$this->field('
				g.id 		gid,
				g.name      gname,
				g.is_new    gnew,
				g.is_hot    ghot,
				g.is_bast   gb,
				g.price     gp,
				g.stock     gs,
				g.description gd,
				g.addtime   gtime,
				gi.name     giname,
				c.name      cname')
			->table('goods 		g,
					goods_img   gi,
					category 	c')
			->where("g.id=gi.goods_id
				and
					c.id=g.cate_id
				and
					g.id=$id
				and
					gi.is_cover=1")
			->select();
			return $list;
		}
	}

?>